package bamin.com.bamindaijia.Activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.aiton.administrator.shane_library.shane.utils.GsonUtils;
import com.aiton.administrator.shane_library.shane.utils.IsQQorWeiXinAvilible;
import com.aiton.administrator.shane_library.shane.widget.TwoBtnDialog;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import bamin.com.bamindaijia.R;
import bamin.com.bamindaijia.constant.Constant;
import bamin.com.bamindaijia.model.Sms;
import bamin.com.bamindaijia.model.User;
import cz.msebera.android.httpclient.Header;

public class KefuActivity extends ZjbBaseActivity implements View.OnClickListener {

    private EditText mEdt_pingjia;
    private TextView mTv_editNum;
    private Button mBtn_sure;
    private User mUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kefu);
        init();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void setListeners() {
        findViewById(R.id.rela_qq).setOnClickListener(this);
        findViewById(R.id.rela_phone).setOnClickListener(this);
        findViewById(R.id.imageView_back).setOnClickListener(this);
        mBtn_sure.setOnClickListener(this);
        mEdt_pingjia.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                mTv_editNum.setText("" + s.length());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected void initSP() {

    }

    @Override
    protected void initIntent() {
        Intent intent = getIntent();
        mUser = (User) intent.getSerializableExtra(Constant.INTENT_KEY.USER);
    }

    @Override
    protected void findID() {
        mEdt_pingjia = (EditText) findViewById(R.id.edt_pingjia);
        mTv_editNum = (TextView) findViewById(R.id.tv_edittextNum);
        mBtn_sure = (Button) findViewById(R.id.button_sure);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageView_back:
                finishTo();
                break;

            case R.id.rela_qq://客服QQ
                boolean qqClientAvailable = IsQQorWeiXinAvilible.isQQClientAvailable(this);
                if (qqClientAvailable) {
                    String url = "mqqwpa://im/chat?chat_type=wpa&uin=3464910568";
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                } else {
                    Toast.makeText(this, "QQ未安装", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.rela_phone://手机
                dialKeFu();
                break;

            case R.id.button_sure:
                final TwoBtnDialog twoBtnDialog = new TwoBtnDialog(this, "确定是否提交反馈", "确定", "取消");
                twoBtnDialog.setClicklistener(new TwoBtnDialog.ClickListenerInterface() {
                    @Override
                    public void doConfirm() {
                        if (mEdt_pingjia.getText().length() == 0) {
                            Toast.makeText(KefuActivity.this, "反馈不能为空", Toast.LENGTH_SHORT).show();
                        } else {

                            pushFanKui();
                        }

                        twoBtnDialog.dismiss();
                    }

                    @Override
                    public void doCancel() {
                        twoBtnDialog.dismiss();
                    }
                });
                twoBtnDialog.show();
                break;
        }
    }

    /**
     * 上传反馈信息
     */
    private void pushFanKui() {
        String url = Constant.Url.ADD_PROPOSE;
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("customerId",mUser.getDatas().getId());
        params.put("customerPhone",mUser.getDatas().getPhone());
        params.put("content",mEdt_pingjia.getText().toString().trim());
        asyncHttpClient.post(url, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                String s = new String(responseBody);
                Log.e("KefuActivity", "反馈: --->>" + s);
                try {
                    Sms sms = GsonUtils.parseJSON(s, Sms.class);
                    if(sms.getStatus() == 0){
                        Toast.makeText(KefuActivity.this, "反馈成功", Toast.LENGTH_SHORT).show();
                        mEdt_pingjia.setText("");
                    }else{
                        Toast.makeText(KefuActivity.this, "反馈失败", Toast.LENGTH_SHORT).show();
                    }

                } catch (Exception e) {
                    Toast.makeText(KefuActivity.this, "数据解析失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                Toast.makeText(KefuActivity.this, "数据请求失败", Toast.LENGTH_SHORT).show();
            }
        });

    }

    /**
     * 拨打客服电话
     */
    private void dialKeFu() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
                != PackageManager.PERMISSION_GRANTED) {
            //申请WRITE_EXTERNAL_STORAGE权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE},
                    Constant.PERMISSION.CALL_PHONE);
        } else {
            final TwoBtnDialog twoBtnDialog = new TwoBtnDialog(this, "是否直接拨打客服电话400-0593-330", "是", "否");
            twoBtnDialog.setClicklistener(new TwoBtnDialog.ClickListenerInterface() {
                @Override
                public void doConfirm() {
                    twoBtnDialog.dismiss();
                    //用intent启动拨打电话
                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "4000593330"));
                    startActivityTo(intent);
                }

                @Override
                public void doCancel() {
                    twoBtnDialog.dismiss();

                }
            });
            twoBtnDialog.show();
        }
    }
}
